/**
 * JAVACC DEMO 1.0
 */
package com.apache.match.queue;

import com.apache.tools.DataMap;
import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * description:
 *
 * @author Administrator 创建时间：2017-1-9
 */
@SuppressWarnings("serial")
public class IndexQueue implements Serializable {

    private static final Logger log = Logger.getLogger(IndexQueue.class);

    private List<QueueDesc> buy = new ArrayList<QueueDesc>();

    private List<QueueAsc> sell = new ArrayList<QueueAsc>();

    public void insertOrUpdate(SupperQueue queue) {
        DataMap param = (DataMap) queue.getObj();
        String orderType = String.valueOf(param.get("orderFlag"));
        if ("buy".equalsIgnoreCase(orderType)) {
            buy.add((QueueDesc) queue);
            Collections.sort(buy);
        } else {
            sell.add((QueueAsc) queue);
            Collections.sort(sell);
        }
    }

    public List getQueue(String orderType) {
        if ("buy".equalsIgnoreCase(orderType)) {
            return buy;
        } else {
            return sell;
        }
    }

    public void removeAll() {
        buy = null;
        sell = null;
    }

    public void setListAll(List<QueueDesc> buy, List<QueueAsc> sell) {
        this.buy = buy;
        this.sell = sell;
    }

    public void remove(String orderType, SupperQueue obj) {
        if ("buy".equalsIgnoreCase(orderType)) {
            buy.remove(obj);
            Collections.sort(buy);
        } else {
            sell.remove(obj);
            Collections.sort(sell);
        }
    }

    public void print() {
        log.info("↓↓↓↓↓*********************************************↓↓↓↓↓");
        if (sell.size() > 0) {
            for (int i = sell.size() - 1; i >= 0; i--) {
                QueueAsc asc = sell.get(i);
                DataMap ascMap = (DataMap) asc.getObj();
                log.info("卖" + (i + 1) + ":price=" + asc.getPriceUnit() + ",orderNum=" + String
                        .valueOf(ascMap.get("orderNum")) + ",dealNum=" + String
                        .valueOf(ascMap.get("dealNum")) + ",orderNo=" + String
                        .valueOf(ascMap.get("orderNo")) + ",times=" + asc.getTimestamp() + "=" + asc
                        .getObj());
            }
        }
        log.info("-------------------------------------");
        if (buy.size() > 0) {
            int j = 0;
            for (QueueDesc desc : buy) {
                j++;
                DataMap descMap = (DataMap) desc.getObj();
                log.info("买" + j + ":price=" + desc.getPriceUnit() + ",orderNum=" + String
                        .valueOf(descMap.get("orderNum")) + ",dealNum=" + String
                        .valueOf(descMap.get("dealNum")) + ",orderNo=" + String
                        .valueOf(descMap.get("orderNo")) + ",times=" + desc.getTimestamp() + "="
                        + desc.getObj());
            }
        }
        log.info("↑↑↑↑↑*********************************************↑↑↑↑↑");
    }

}
